package com.sanjiahantang.youjiarole.module.cases.views.impl;

import android.content.Intent;
import android.text.TextUtils;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.sanjiahantang.uilibrary.adapter.listview.QuickAdapter;
import com.sanjiahantang.youjiarole.R;
import com.sanjiahantang.youjiarole.base.BaseMvpActivity;
import com.sanjiahantang.youjiarole.module.cases.presenters.CasePresenter;
import com.sanjiahantang.youjiarole.module.cases.views.CaseView;


/**
 * Created by zy on 2017/8/23.
 * 案例
 */

@SuppressWarnings("deprecation")
public class CaseActivity extends BaseMvpActivity<CaseView,CasePresenter> implements View.OnClickListener,CaseView,CaseView.OnListLoadListener {

    private ListView lvCases;

    private View ffStyle,ffRoomType,ffPrice,ffAddress,llMask,back;
    private TextView txtStyle,txtRoomType,txtPrice,txtAddress,txtKeyWords;
    private ImageView ivStyle,ivRoomType,ivPrice,ivAddress;
    private ListView optionList,addressList;


    private int colorPrimary,text_gary_color;


    @Override
    public CasePresenter initPresenter() {
        return new CasePresenter();
    }

    @Override
    public void initView() {
        setContentView(R.layout.activity_case);

        colorPrimary = $this.getResources().getColor(R.color.colorPrimary);
        text_gary_color = $this.getResources().getColor(R.color.gray);
        lvCases = (ListView) $this.findViewById(R.id.lvCases);
        optionList = (ListView) $this.findViewById(R.id.optionList);
        addressList = (ListView) $this.findViewById(R.id.addressList);
        presenter.setOnListLoadListener(this,this);
        ffStyle = $this.findViewById(R.id.ffStyle);
        ffRoomType = $this.findViewById(R.id.ffRoomType);
        ffPrice = $this.findViewById(R.id.ffPrice);
        ffAddress = $this.findViewById(R.id.ffAddress);
        llMask = $this.findViewById(R.id.llMask);
        txtStyle = (TextView) $this.findViewById(R.id.txtStyle);
        txtRoomType = (TextView) $this.findViewById(R.id.txtRoomType);
        txtPrice = (TextView) $this.findViewById(R.id.txtPrice);
        txtAddress = (TextView) $this.findViewById(R.id.txtAddress);
        ivStyle = (ImageView) $this.findViewById(R.id.ivStyle);
        ivRoomType = (ImageView) $this.findViewById(R.id.ivRoomType);
        ivPrice = (ImageView) $this.findViewById(R.id.ivPrice);
        ivAddress = (ImageView) $this.findViewById(R.id.ivAddress);

        txtKeyWords = (TextView) $this.findViewById(R.id.txtKeyWords);
        back = $this.findViewById(R.id.back);

        optionList.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                if (motionEvent.getAction() == MotionEvent.ACTION_MOVE){
                    return false;
                }
                recoverDefault();
                return false;
            }
        });

        addressList.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                if (motionEvent.getAction() == MotionEvent.ACTION_MOVE || motionEvent.getAction() == MotionEvent.ACTION_UP) {
                    return false;
                }
                recoverDefault();
                return false;
            }
        });
        $this.findViewById(R.id.llSearchBar).setOnClickListener(this);
    }


    @Override
    public void initData() {

        ffStyle.setOnClickListener(this);
        ffRoomType.setOnClickListener(this);
        ffPrice.setOnClickListener(this);
        ffAddress.setOnClickListener(this);
        llMask.setOnClickListener(this);
        back.setOnClickListener(this);
        presenter.loadCases(this, null);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.ffStyle:
                if(llMask.getVisibility() == View.GONE){
                    onSelectorClicked(txtStyle,ivStyle,TYPE_STYLE);
                }else{
                    recoverDefault();
                }
                break;
            case R.id.ffRoomType:
                if(llMask.getVisibility() == View.GONE){
                    onSelectorClicked(txtRoomType,ivRoomType,TYPE_ROOM_TYPE);
                }else{
                    recoverDefault();
                }
                break;
            case R.id.ffPrice:
                if(llMask.getVisibility() == View.GONE){
                    onSelectorClicked(txtPrice,ivPrice,TYPE_PRICE);
                }else{
                    recoverDefault();
                }
                break;
            case R.id.ffAddress:
                if (llMask.getVisibility() == View.GONE) {
                    onSelectorClicked(txtAddress, ivAddress, TYPE_ADDRESS);
                } else {
                    recoverDefault();
                }
                break;
            case R.id.llMask:
                recoverDefault();
                break;
            case R.id.back:
                if(llMask.getVisibility() == View.GONE){
                    finish();
                }else{
                    recoverDefault();
                }
                break;
            case R.id.llSearchBar:
                if(llMask.getVisibility() == View.GONE){
                    presenter.goSearchPage($this);
                }else{
                    recoverDefault();
                }
                break;
        }
    }

    private void onSelectorClicked(TextView tv, ImageView iv, int type){
        recoverDefault();
        presenter.resetAdapterData($this,type);
        tv.setTextColor(colorPrimary);
        iv.setImageResource(R.drawable.i_up_green);
        llMask.setVisibility(View.VISIBLE);
        if (type == TYPE_ADDRESS) {
            addressList.setVisibility(View.VISIBLE);
            addressList.bringToFront();
            optionList.setVisibility(View.GONE);
        } else {
            addressList.setVisibility(View.GONE);
            optionList.setVisibility(View.VISIBLE);
            optionList.bringToFront();
        }
    }

    @Override
    public void recoverDefault(){
        txtStyle.setTextColor(text_gary_color);
        txtRoomType.setTextColor(text_gary_color);
        txtPrice.setTextColor(text_gary_color);
        txtAddress.setTextColor(text_gary_color);

        ivStyle.setImageResource(R.drawable.i_down_gray);
        ivRoomType.setImageResource(R.drawable.i_down_gray);
        ivPrice.setImageResource(R.drawable.i_down_gray);
        ivAddress.setImageResource(R.drawable.i_down_gray);

        llMask.setVisibility(View.GONE);
        optionList.setVisibility(View.GONE);
        addressList.setVisibility(View.GONE);
    }


    @Override
    public void showProgress() {
        super.showProgress($this);
    }


//    @Override
//    public void initContainers(QuickAdapter selectorAdapter, QuickAdapter caseAdapter) {
//        optionList.setAdapter(selectorAdapter);
//        lvCases.setAdapter(caseAdapter);
//    }
    @Override
    public void initContainers(QuickAdapter selectorAdapter, QuickAdapter caseAdapter, QuickAdapter addressAdapter) {
        addressList.setAdapter(addressAdapter);
        optionList.setAdapter(selectorAdapter);
        lvCases.setAdapter(caseAdapter);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == INTENT_CASE && resultCode == RESULT_OK){
            String keyWords = data.getStringExtra(CaseView.KEY_CASE_TITLE);
            if (TextUtils.isEmpty(keyWords)){
                txtKeyWords.setText(R.string.key_words_hint);
            }else {
                txtKeyWords.setText(keyWords);
            }
            presenter.loadCases($this, keyWords);
        }
    }

    @Override
    public void setTextSelected(String parentId, String dicName, String dicValue) {
        if (CaseView.KEY_TYPES[0].equals(parentId)){
            if ("全部".equals(dicName)){
                dicName = "风格";
            }
            txtStyle.setText(dicName);
        }else if (CaseView.KEY_TYPES[1].equals(parentId)){
            if ("全部".equals(dicName)){
                dicName = "户型";
            }
            txtRoomType.setText(dicName);
        }else if (CaseView.KEY_TYPES[2].equals(parentId)){
            if ("全部".equals(dicName)){
                dicName = "价格区间";
            }
            txtPrice.setText(dicName);
        }else if (CaseView.KEY_TYPES[3].equals(parentId)){
            if ("全部".equals(dicName)){
                dicName = "地区";
            }
            txtAddress.setText(dicName);
        }
    }
}
